package com.ruoyi.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import com.ruoyi.domain.AdvPlatinfos;
import com.ruoyi.domain.AdvProducts;
import com.ruoyi.mapper.AdvProductsMapper;
import com.ruoyi.service.IAdvProductsService;
import com.ruoyi.utils.MyFileUtils;
import com.ruoyi.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;

/**
 *  Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-13
 */
@Service
public class AdvProductsServiceImpl implements IAdvProductsService
{
    @Autowired
    private AdvProductsMapper advProductsMapper;

    /**
     * 查询
     *
     * @param advProductsId  主键
     * @return
     */
    @Override
    public AdvProducts selectAdvProductsByAdvProductsId(Long advProductsId)
    {
        return advProductsMapper.selectAdvProductsByAdvProductsId(advProductsId);
    }

    /**
     * 查询 列表
     *
     * @param advProducts
     * @return
     */
    @Override
    public List<AdvProducts> selectAdvProductsList(AdvProducts advProducts)
    {
        return advProductsMapper.selectAdvProductsList(advProducts);
    }

    @Override
    public List<AdvProducts> selectAdvProductsListApp(AdvProducts advProducts)
    {
        return advProductsMapper.selectAdvProductsListApp(advProducts);
    }

    /**
     * 新增
     *
     * @param advProducts
     * @return 结果
     */
    @Override
    public int insertAdvProducts(AdvProducts advProducts)
    {
        advProducts.setAdvProductsId(UUIDUtils.timeMillisId());
        advProducts.setPutonsaleTime(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").format(new Date()));
        advProducts.setStocknum(0L);
        return advProductsMapper.insertAdvProducts(advProducts);
    }

    /**
     * 修改
     *
     * @param advProducts
     * @return 结果
     */
    @Override
    public int updateAdvProducts(AdvProducts advProducts)
    {
        advProductsMapper.deleteByDevicerelation(advProducts.getAdvProductsId());
        return advProductsMapper.updateAdvProducts(advProducts);
    }

    /**
     * 批量删除
     *
     * @param advProductsIds 需要删除的 主键
     * @return 结果
     */
    @Override
    public int deleteAdvProductsByAdvProductsIds(Long[] advProductsIds)
    {
        List<AdvProducts> advPlatinfosList = new ArrayList<AdvProducts>();
        for (Long productId : advProductsIds) {
            AdvProducts platinfos = advProductsMapper.selectAdvProductsByAdvProductsId(productId);
            if(!Objects.isNull(platinfos)) {
                advPlatinfosList.add(platinfos);
            }
        }
        int i = 0;
        try {
            i = advProductsMapper.deleteByTag(advProductsIds);
        }catch (DataIntegrityViolationException e){
            throw new RuntimeException("请先删除关联数据");
        }
        if(i > 0) {
            for (AdvProducts products : advPlatinfosList) {
                MyFileUtils.deleteFile(products.getProductImage());
                MyFileUtils.deleteFile(products.getDetailImage());
                MyFileUtils.deleteFile(products.getSceneImage1());
                MyFileUtils.deleteFile(products.getSceneImage2());
                MyFileUtils.deleteFile(products.getProductUrl());
            }
        }
        return i;
    }

    /**
     * 删除 信息
     *
     * @param advProductsId  主键
     * @return 结果
     */
    @Override
    public int deleteAdvProductsByAdvProductsId(Long advProductsId)
    {
        try {
            return advProductsMapper.deleteAdvProductsByAdvProductsId(advProductsId);
        }catch (DataIntegrityViolationException e){
            throw new RuntimeException("请先删除关联数据");
        }

    }

    @Override
    public List<AdvProducts> findcollect(Long advMembersIds, Long productType) {
        List<AdvProducts> products = advProductsMapper.findcollect(advMembersIds, productType);
        return products;
    }

    @Override
    public List<AdvProducts> findBuyWithDevicesno(String devicesno, String advMembersId) {
        List<AdvProducts> products = advProductsMapper.findBuyWithDevicesno(devicesno, advMembersId);
        return products;
    }

    @Override
    public AdvProducts isBuy(Long advMembersId, Long advProductsId) {
        AdvProducts products = advProductsMapper.isBuy(advMembersId, advProductsId);
        return products;
    }

    @Override
    public List<AdvProducts> getOrderProduct() {
        List<AdvProducts> advProducts = advProductsMapper.getOrderProduct();
        return advProducts;
    }


}
